﻿using Note.Model;
using System.Windows;

namespace Note.View
{
    /// <summary>
    /// Interaction logic for EditNoteDialog.xaml
    /// </summary>
    public partial class EditNoteDialog : Window
    {
        public EditNoteDialog()
        {
            InitializeComponent();
        }

        public bool Value { get; set; }

        private NoteDetailModel _model;
        public NoteDetailModel Model
        {
            get
            {
                return _model;
            }
            set
            {
                _model = value;
            }
        }

        private void Cancel(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Confirm(object sender, RoutedEventArgs e)
        {
            if (Value)
            {
                Model = new NoteDetailModel()
                {
                    Type = NoteType.Text,
                    Title = NoteTitle.Text,
                    Content = NoteContent.Text
                };
                NoteModel.AddNoteDetailToList(Model);
            }
            else
            {
                if (Model.Title == NoteTitle.Text && Model.Type == NoteType.Text)
                {
                    Model.Content = NoteContent.Text;
                }
                else
                {
                    this.DeleteFile(Model.Path);
                    Model = new NoteDetailModel()
                    {
                        Type = NoteType.Text,
                        Title = NoteTitle.Text,
                        Content = NoteContent.Text
                    };
                    NoteModel.AddNoteDetailToList(Model);
                }
            }
            this.DialogResult = true;
            this.Close();
        }

        private void Dialog_Loaded(object sender, RoutedEventArgs e)
        {
            Title = Value ? "Create Note" : "Edit Note";
            if (Model != null)
            {
                this.NoteTitle.Text = Model.Title;
                this.NoteType.Text = Model.Type;
                this.NoteContent.Text = Model.Content;
            }
        }
    }
}
